########
# Usage: Rscript detail_circRNA_boxplot3.R 'exo_circ_001570' "TRUE" "wilcox.test" "Health,HCC"
#        Rscript detail_circRNA_boxplot3.R 'exo_circ_001570' "FALSE" "wilcox.test" "Health,HCC"
# 传入参数： target = 'exo_circ_001570'  
#            log = "TRUE"                    # log = "FALSE"   or   log = "TRUE"
#            testMethod = "wilcox.test"      # testMethod = "wilcox.test"   or   testMethod = "t.test"
#            cohort.selected = c('Health', 'HCC')
########
library(ggplot2)
library(ggpubr)
library(RColorBrewer)

library(optparse)

option_list <- list(
  make_option("--s", default = "", type = "character", help = "sample file"),
  make_option("--r", default = "", type = "character", help = "rna file"),
  make_option("--t", default = "", type = "character", help = "target"),
  make_option("--l", default = "", type = "character", help = "target"),
  make_option("--m", default = "", type = "character", help = "test method"),
  make_option("--c", default = "", type = "character", help = "cohort selected"),
  make_option("--o", default = "", type = "character", help = "output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))


Args = commandArgs()
target = opt$t
log = opt$l
testMethod = opt$m
cohort.selected = opt$c
cohort.selected = as.vector(unlist(strsplit(cohort.selected, ',')))
cohort.selected
#target = 'exo_circ_001570'
#log = "TRUE"                   # log = "FALSE"   or   log = "TRUE"
#testMethod = "wilcox.test"      # testMethod = "wilcox.test"   or   testMethod = "t.test"
#cohort.selected = c('Health', 'HCC')

#path = "D:\\exo\\889\\submit\\detail_circRNA"
#setwd(path)
file.sample = opt$s
file.RNA = opt$r
targetType = 'circRNA'
w=6
h=6
outfile = opt$o

data.sample = read.csv(file.sample, sep='\t', header = TRUE, row.names=NULL)
samples = data.sample$sample
samplesNum = length(samples)
print(samplesNum)
cohorts = unique(data.sample$cohort)
print(samplesNum)
print(length(cohorts))

data = read.csv(file.RNA, sep=',', header = TRUE, row.names=1)
data.selected = data[target, ]
data.selected = as.data.frame(t(data.selected))
data.selected$sample = row.names(data.selected)

data.target = merge(data.sample, data.selected, by = 'sample')
colnames(data.target) = c('sample', 'type', 'cohort', 'TPM')
data.target = data.target[data.target$cohort %in% cohort.selected, ]

cols = c(brewer.pal(9,"Pastel1"), brewer.pal(8,"Set3"))
my_comparisons <- list(cohort.selected)
if(log == "FALSE"){
  pdf(outfile,  width=w, height=h)
  #ylim = boxplot.stats(data.target$TPM, coef = 2)$stats[c(1, 5)]
  p = ggplot(data.target, aes(x = cohort, y = TPM, fill=cohort))+
    geom_boxplot(linetype = "dashed", width=0.6, outlier.shape = 1, outlier.size = 0.8, outlier.colour = 'black', color="#525252", size=0.5) +
    stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..), width=0.6, outlier.shape = 1, outlier.size = 0.8, outlier.colour = 'black', color="#525252", size=0.5) +
    stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..), color="#525252", size=0.5, width=0.3) +
    stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..), color="#525252", size=0.5, width=0.3)+
    stat_compare_means(comparisons = my_comparisons, method=testMethod,label = "p.signif")+
    scale_fill_manual(values = cols)+
    xlab('')+ ylab('CPM')+
    ggtitle(target)+
    theme_bw() + 
    theme(panel.grid.major.x = element_blank(),
          panel.grid= element_line(colour = 'gray'))+
    theme(axis.title = element_text(size = 12, face = "bold"))+
    theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5))+
    guides(fill = FALSE, colour = FALSE)+
    theme(panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA),
          axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
          axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 45, hjust = 1),
          axis.title = element_text(size = 14, face = "bold"))
  #p = p + coord_cartesian(ylim = c(0, ylim[2]*1.05))
}else{
  data.target$TPM = log2(data.target$TPM+1)
  pdf(outfile,  width=w, height=h)
  #ylim = boxplot.stats(data.target$TPM, coef = 2)$stats[c(1, 5)]
  p = ggplot(data.target, aes(x = cohort, y = TPM, fill=cohort))+
    geom_boxplot(linetype = "dashed", width=0.6, outlier.shape = 1, outlier.size = 0.8, outlier.colour = 'black', color="#525252", size=0.5) +
    stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..), width=0.6, outlier.shape = 1, outlier.size = 0.8, outlier.colour = 'black', color="#525252", size=0.5) +
    stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..), color="#525252", size=0.5, width=0.3) +
    stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..), color="#525252", size=0.5, width=0.3)+
    stat_compare_means(comparisons = my_comparisons, method=testMethod,label = "p.signif")+
    scale_fill_manual(values = cols)+
    xlab('')+ ylab('log2(CPM+1)')+
    ggtitle(target)+
    theme_bw() + 
    theme(panel.grid.major.x = element_blank(),
          panel.grid= element_line(colour = 'gray'))+
    theme(axis.title = element_text(size = 12, face = "bold"))+
    theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5))+
    guides(fill = FALSE, colour = FALSE)+
    theme(panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA),
          axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
          axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 45, hjust = 1),
          axis.title = element_text(size = 14, face = "bold"))
  #p = p + coord_cartesian(ylim = c(0, ylim[2]*1.05))
}
print(p)
dev.off()  





